package com.prp.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.prp.struts.bean.PageValue;

/**
 * A data access object (DAO) providing persistence and search support for Item
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.prp.dao.Item
 * @author MyEclipse Persistence Tools
 */

public class ItemDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(ItemDAO.class);
	// property constants
	public static final String FILE_NAME = "fileName";
	public static final String FILE_NAME_CH = "fileNameCh";
	public static final String PUBLISH_ORGAN = "publishOrgan";
	public static final String PUBLISH_ORGAN_CH = "publishOrganCh";
	public static final String MAIN_CONTENT = "mainContent";
	public static final String MAIN_CONTENT_CH = "mainContentCh";
	public static final String DATA_SOURCE = "dataSource";
	public static final String IS_ALL = "isAll";
	public static final String LANGUAGE = "language";
	public static final String TYPE = "type";
	public static final String FILE_PATH = "filePath";
	public static final String STATE = "state";
	public static final String VIEW_NUM = "viewNum";
	public static final String DOWN_NUM = "downNum";
	public static final String COUNTRY = "country";

	public void save(Item transientInstance) {
		log.debug("saving Item instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Item persistentInstance) {
		log.debug("deleting Item instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Item findById(java.lang.Integer id) {
		log.debug("getting Item instance with id: " + id);
		try {
			Item instance = (Item) getSession().get("com.prp.dao.Item", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Item instance) {
		log.debug("finding Item instance by example");
		try {
			List results = getSession().createCriteria("com.prp.dao.Item").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Item instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Item as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByFileName(Object fileName) {
		return findByProperty(FILE_NAME, fileName);
	}

	public List findByFileNameCh(Object fileNameCh) {
		return findByProperty(FILE_NAME_CH, fileNameCh);
	}

	public List findByPublishOrgan(Object publishOrgan) {
		return findByProperty(PUBLISH_ORGAN, publishOrgan);
	}

	public List findByPublishOrganCh(Object publishOrganCh) {
		return findByProperty(PUBLISH_ORGAN_CH, publishOrganCh);
	}

	public List findByMainContent(Object mainContent) {
		return findByProperty(MAIN_CONTENT, mainContent);
	}

	public List findByMainContentCh(Object mainContentCh) {
		return findByProperty(MAIN_CONTENT_CH, mainContentCh);
	}

	public List findByDataSource(Object dataSource) {
		return findByProperty(DATA_SOURCE, dataSource);
	}

	public List findByIsAll(Object isAll) {
		return findByProperty(IS_ALL, isAll);
	}

	public List findByLanguage(Object language) {
		return findByProperty(LANGUAGE, language);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByFilePath(Object filePath) {
		return findByProperty(FILE_PATH, filePath);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByViewNum(Object viewNum) {
		return findByProperty(VIEW_NUM, viewNum);
	}

	public List findByDownNum(Object downNum) {
		return findByProperty(DOWN_NUM, downNum);
	}

	public List findByCountry(Object country) {
		return findByProperty(COUNTRY, country);
	}

	public List findAll() {
		log.debug("finding all Item instances");
		try {
			String queryString = "from Item";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Item merge(Item detachedInstance) {
		log.debug("merging Item instance");
		try {
			Item result = (Item) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Item instance) {
		log.debug("attaching dirty Item instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Item instance) {
		log.debug("attaching clean Item instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public List findAllByPage(String searchinfo, String coun, int pageSize, int pageNow){
		log.debug("finding all Item instances by page");
		try {
			
			String queryString = " from Item as m where m.fileName like '%'||:name||'%' "		
				+ " or m.fileNameCh like '%'||:name||'%' "
				+ " or m.publishOrgan like '%'||:name||'%' "
				+ " or m.publishOrganCh like '%'||:name||'%' "
				+ " or m.mainContent like '%'||:name||'%' "
				+ " or m.mainContentCh like '%'||:name||'%' ";
			
			if (coun != null)
				queryString += " or m.country = :kkname ";
			
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(pageNow*pageSize - pageSize);
			queryObject.setMaxResults(pageSize);
			queryObject.setParameter("name", searchinfo);
			if (coun != null){
				queryObject.setParameter("kkname", coun);
			}
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAll(String searchinfo, String coun){
		log.debug("finding all Item instances by page");
		try {
			
			String queryString = " from Item as m where m.fileName like '%'||:name||'%' "		
				+ " or m.fileNameCh like '%'||:name||'%' "
				+ " or m.publishOrgan like '%'||:name||'%' "
				+ " or m.publishOrganCh like '%'||:name||'%' "
				+ " or m.mainContent like '%'||:name||'%' "
				+ " or m.mainContentCh like '%'||:name||'%' ";
			
			if (coun != null)
				queryString += " or m.country = :kkname ";
			
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter("name", searchinfo);
			if (coun != null){
				queryObject.setParameter("kkname", coun);
			}
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List searchFileByPage(String searchinfo, String coun, Integer stat, PageValue size){
		log.debug("finding all Item instances by page");
		try {
			if(searchinfo == null)
				searchinfo = "";
			
			String queryString = " from Item as m where (m.fileName like '%'||:name||'%' "		
				+ " or m.fileNameCh like '%'||:name||'%' "
				+ " or m.publishOrgan like '%'||:name||'%' "
				+ " or m.publishOrganCh like '%'||:name||'%' "
				+ " or m.mainContent like '%'||:name||'%' "
				+ " or m.mainContentCh like '%'||:name||'%' ";
			
			if (coun != null)
				queryString += " or m.country = :kkname ";
			
			queryString+=")";
			if (stat != null)
				queryString += " and m.state =:stat ";
			
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter("name", searchinfo);
			if (coun != null){
				queryObject.setParameter("kkname", coun);
			}
			if (stat != null)
				queryObject.setParameter("stat", stat);
			
			
			List temp = queryObject.list();
			
			size.setPageCount(queryObject.list().size());
			int pageCount = size.getPageCount();
			int pageSize = size.getPageSize();
			int pageNow = size.getPageNow();
			if(pageNow <= 0)
				pageNow = 1;
			
			if(pageCount%pageSize == 0){
				if(pageNow > pageCount/pageSize)
					pageNow = pageCount / pageSize;
			}else{
				if(pageNow > pageCount/pageSize + 1)
					pageNow = pageCount / pageSize + 1;
			}
			size.setPageNow(pageNow);
			size.setPageSize(pageSize);
			queryObject.setFirstResult(pageNow*pageSize - pageSize);
			queryObject.setMaxResults(pageSize);
			
			
			return queryObject.list();
			
			
			
			
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}